import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CommPasswordFormFieldWidget extends StatefulWidget {
  Color? cursorColor;
  Function? onChanged;
  String? defaultCont;
  TextStyle? style;
  TextStyle? hintStyle;
  TextStyle? labelStyle;
  String? labelText;
  String? hintText;
  bool? isShowPrefixIcon;
  Widget? prefixIcon;
  EdgeInsetsGeometry? contentPadding;

  CommPasswordFormFieldWidget({super.key,
    this.cursorColor,
    this.defaultCont,
    this.onChanged,
    this.hintStyle,
    this.style,
    this.labelStyle,
    this.labelText,
    this.hintText,
    this.isShowPrefixIcon,
    this.prefixIcon,
    this.contentPadding,
  });

  @override
  State<CommPasswordFormFieldWidget> createState() => _CommPasswordFormFieldWidgetState();
}

class _CommPasswordFormFieldWidgetState extends State<CommPasswordFormFieldWidget> {
  late TextEditingController _editingController;
  RxBool isObscure = true.obs;
  @override
  void initState() {
    super.initState();
    _editingController=TextEditingController(text: widget.defaultCont);
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() => TextField(
      controller: _editingController,
      onChanged: (value){
        if(widget.onChanged!=null)widget.onChanged!(value);
      },
      obscureText: isObscure.value,
      style: widget.style??TextStyle(
          fontSize: px_18.sp,
          color: color_333333
      ),
      decoration: InputDecoration(
        contentPadding: widget.contentPadding??EdgeInsets.symmetric(vertical: px_2.h),
        labelText: widget.labelText??string_password,
        labelStyle: widget.labelStyle??TextStyle(
            fontSize: px_18.sp,
            color: color_999999
        ),
        hintText: widget.hintText??string_please_enter_password,
        hintStyle: widget.hintStyle??TextStyle(
            fontSize: px_18.sp,
            color: color_999999
        ),
        // floatingLabelBehavior: FloatingLabelBehavior.never,
        enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(px_6.sp),
            borderSide: BorderSide(
                color: color_cccccc
            )
        ),
        focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(px_6.sp),
            borderSide: BorderSide(
                color: color_cccccc
            )
        ),
        // filled: true,
        // fillColor: Color(0xFFF8F8F8),
        prefixIcon: !(widget.isShowPrefixIcon??true)?null:
        (widget.prefixIcon??Icon(
          Icons.lock,
          size: px_32.sp,
          color: color_999999,
        )),
        suffixIcon: IconButton(
          icon: isObscure.value ? Icon(
            Icons.visibility_off_outlined,
            color: color_666666,
            size: px_16,
          ) :
          Icon(
              Icons.visibility_outlined,
            color: color_666666,
            size: px_16,
          ),
          onPressed: ()=>(isObscure.value = !(isObscure.value)),
        ),
      ),
      cursorColor: widget.cursorColor,
    ));
  }
}
